package com.thomas.generator.util;

import org.apache.commons.io.FileUtils;

import java.io.*;

/**
 * @author xutao
 * @Package com.thomas.generator.util
 * @Description: ${todo}
 * @date 2019/12/11 10:28
 */
public class FileUtil {

    static String distPath = PropertiesUtil.get("generator.distPath");
    static String srcPath = FreemarkerUtil.path+"/dist/";
    static String basePackege = PropertiesUtil.get("project.package.prefix");
    static String moduleName = PropertiesUtil.get("project.moduleName");


    public static void runCopy(){
        // 1 copy java file
        File srcJavaFile = new File(srcPath+moduleName);
        File destJavaFile = new File(distPath+"/src/main/java/"+basePackege.replace(".", "/")+"/"+moduleName);
        if(srcJavaFile.exists()){
            copy(srcJavaFile,destJavaFile);
        }
        // 2 copy mapper
        File srcMapperFile = new File(srcPath+"/mapping/"+moduleName);
        File destMapperFile = new File(distPath+"/src/main/resources/mapping/"+moduleName);
        if(srcMapperFile.exists()){
            copy(srcMapperFile,destMapperFile);
        }
    }
    //删除生成的代码
    public static void runSrcDelete(){
        File srcFile = new File(srcPath);
        if(srcFile.exists()){
            boolean b = FileUtils.deleteQuietly(srcFile);
            System.out.printf("\n delete srcFile %s  result is %s \n",srcPath,b);
        }
    }

    public static void copy(File srcDir,File destDir){
        if(!srcDir.exists()){
            return;
        }
        try {
            FileUtils.copyDirectory(srcDir, destDir);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }




}
